#include "money_manager.h"
#include <sstream>
#include <iomanip>
#include <cstring>
#include <iostream>

// 静态变量
static double currentMoney = 0.0;
static const double INITIAL_MONEY = 550000.0;  // 20万初始资金
static char lastOperationInfo[256];  // 存储最后一次操作的信息
static bool isInitialized = false;

// 持仓相关变量
static int positionCount = 0;        // 持仓数量
static double positionValue = 0.0;   // 持仓市值
static double positionProfit = 0.0;  // 持仓盈亏
static char positionInfo[256];       // 持仓信息字符串

// 格式化金额显示
std::string formatMoney(double amount) {
    std::stringstream ss;
    ss << std::fixed << std::setprecision(2) << amount;
    return ss.str();
}

// 更新持仓信息
void updatePositionInfo() {
    std::stringstream ss;
    ss << "持仓数量: " << positionCount << " 股, "
       << "持仓市值: ¥" << formatMoney(positionValue) << ", "
       << "持仓盈亏: ¥" << formatMoney(positionProfit);
    
    strncpy(positionInfo, ss.str().c_str(), sizeof(positionInfo) - 1);
    positionInfo[sizeof(positionInfo) - 1] = '\0';
}

// 更新操作信息
void updateOperationInfo(const std::string& operation, double amount, const char* reason) {
    std::stringstream ss;
    ss << operation << " ¥" << formatMoney(amount);
    if (reason && strlen(reason) > 0) {
        ss << " (" << reason << ")";
    }
    ss << ", 当前余额: ¥" << formatMoney(currentMoney);
    
    strncpy(lastOperationInfo, ss.str().c_str(), sizeof(lastOperationInfo) - 1);
    lastOperationInfo[sizeof(lastOperationInfo) - 1] = '\0';
}

// 初始化资金管理器
MONEY_API void initMoneyManager() {
    if (!isInitialized) {
        currentMoney = INITIAL_MONEY;
        positionCount = 0;
        positionValue = 0.0;
        positionProfit = 0.0;
        updateOperationInfo("初始化资金", INITIAL_MONEY, "");
        updatePositionInfo();
        isInitialized = true;
    }
}

// 获取当前资金
MONEY_API double getCurrentMoney() {
    if (!isInitialized) {
        initMoneyManager();
    }
    return currentMoney;
}

// 减少资金
MONEY_API bool decreaseMoney(double amount, const char* reason) {
    // std::cout << "减少资金: " << amount << " 理由: " << reason << std::endl;
    if (!isInitialized) {
        initMoneyManager();
    }

    if (amount <= 0) {
        strncpy(lastOperationInfo, "错误：减少金额必须大于0", sizeof(lastOperationInfo) - 1);
        return false;
    }

    if (amount > currentMoney) {
        std::stringstream ss;
        ss << "错误：资金不足（当前余额: ¥" << formatMoney(currentMoney) 
           << "，需要: ¥" << formatMoney(amount) << "）";
        strncpy(lastOperationInfo, ss.str().c_str(), sizeof(lastOperationInfo) - 1);
        return false;
    }

    currentMoney -= amount;
    updateOperationInfo("支出", amount, reason);
    // std::cout << "减少资金成功: " << amount << " 理由: " << reason << std::endl;
    return true;
}

// 增加资金
MONEY_API void increaseMoney(double amount, const char* reason) {
    if (!isInitialized) {
        initMoneyManager();
    }

    if (amount <= 0) {
        strncpy(lastOperationInfo, "错误：增加金额必须大于0", sizeof(lastOperationInfo) - 1);
        return;
    }

    currentMoney += amount;
    updateOperationInfo("收入", amount, reason);
}

// 获取上一次操作的结果信息
MONEY_API const char* getLastOperationInfo() {
    if (!isInitialized) {
        return "资金管理器尚未初始化";
    }
    return lastOperationInfo;
} 